package com.smartscreen.attendancemanage.service.impl;

import com.smartscreen.attendancemanage.dto.ApprovalLeaveRequest;
import com.smartscreen.attendancemanage.dto.CreateLeaveApplicationRequest;
import com.smartscreen.attendancemanage.dto.LeaveApplicationWithStudentInfoDTO;
import com.smartscreen.attendancemanage.dto.StudentInfoDTO;
import com.smartscreen.attendancemanage.entity.LeaveApplication;
import com.smartscreen.attendancemanage.mapper.LeaveApplicationMapper;
import com.smartscreen.attendancemanage.service.LeaveApplicationService;
import com.smartscreen.attendancemanage.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LeaveApplicationServiceImpl implements LeaveApplicationService {
    
    @Autowired
    private LeaveApplicationMapper leaveApplicationMapper;
    
    @Autowired
    private StudentInfoService studentInfoService;
    
    @Override
    public String createLeaveApplication(CreateLeaveApplicationRequest request) {
        String applicationId = "leave_appl_" + UUID.randomUUID().toString().substring(0, 8);
        
        LeaveApplication application = new LeaveApplication();
        application.setApplicationId(applicationId);
        application.setStudentId(request.getStudentId());
        application.setLeaveType(request.getLeaveType());
        application.setStartTime(request.getStartTime());
        application.setEndTime(request.getEndTime());
        application.setReason(request.getReason());
        application.setStatus("pending");
        
        leaveApplicationMapper.insert(application);
        
        // 这里还需要插入附件数据
        // 为了简化，暂时省略
        
        return applicationId;
    }
    
    @Override
    public List<LeaveApplication> getPendingApplications() {
        return leaveApplicationMapper.getPendingApplications();
    }
    
    @Override
    public List<LeaveApplicationWithStudentInfoDTO> getPendingApplicationsWithStudentInfo() {
        // 获取待审批的请假申请
        List<LeaveApplication> applications = leaveApplicationMapper.getPendingApplications();
        
        if (applications.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 提取学生ID列表
        List<String> studentIds = applications.stream()
                .map(LeaveApplication::getStudentId)
                .distinct()
                .collect(Collectors.toList());
        
        // 批量获取学生信息
        Map<String, StudentInfoDTO> studentInfoMap = studentInfoService.getStudentsBatch(studentIds);
        
        // 组合数据
        return applications.stream().map(application -> {
            LeaveApplicationWithStudentInfoDTO dto = new LeaveApplicationWithStudentInfoDTO(application);
            
            // 填充学生信息
            StudentInfoDTO studentInfo = studentInfoMap.get(application.getStudentId());
            if (studentInfo != null) {
                dto.setStudentName(studentInfo.getName());
                dto.setStudentNumber(studentInfo.getStudentNumber());
            } else {
                dto.setStudentName("未知学生");
                dto.setStudentNumber("");
            }
            
            // 如果需要审批人信息，也可以通过类似方式获取teacher信息
            // 这里暂时省略，因为teacher表可能在其他模块
            
            return dto;
        }).collect(Collectors.toList());
    }
    
    @Override
    public void approveLeaveApplication(String applicationId, ApprovalLeaveRequest request) {
        LeaveApplication application = new LeaveApplication();
        application.setApplicationId(applicationId);
        application.setStatus(request.getStatus());
        application.setApprovalComment(request.getComment());
        application.setApprovalTime(LocalDateTime.now());
        
        leaveApplicationMapper.updateById(application);
    }
    
    @Override
    public List<LeaveApplicationWithStudentInfoDTO> getApplicationsByStudentId(String studentId) {
        // 获取指定学生的请假申请
        List<LeaveApplication> applications = leaveApplicationMapper.getApplicationsByStudentId(studentId);
        
        if (applications.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取学生信息
        StudentInfoDTO studentInfo = studentInfoService.getStudentInfo(studentId);
        
        // 组合数据
        return applications.stream().map(application -> {
            LeaveApplicationWithStudentInfoDTO dto = new LeaveApplicationWithStudentInfoDTO(application);
            
            // 填充学生信息
            if (studentInfo != null) {
                dto.setStudentName(studentInfo.getName());
                dto.setStudentNumber(studentInfo.getStudentNumber());
            } else {
                dto.setStudentName("未知学生");
                dto.setStudentNumber("");
            }
            
            return dto;
        }).collect(Collectors.toList());
    }
}